﻿using brand.admin.cyt369.com.services.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace brand.admin.cyt369.com.services.Services
{
    public class CategoryService: admin.cyt369.com.services.ORM.BaseService<CategoryService>
    {
        /// <summary>
        /// 获取类型通过id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public brand.admin.cyt369.com.services.Models.Category GetCategoryByID(int id)
        {
            using (var ctx = this.Ctx)
            {
                var category =ctx.Categories.FirstOrDefault(c => c.Id == id);

                return category;
            }
        }
        /// <summary>
        /// 获取类别通过 父级ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IQueryable<brand.admin.cyt369.com.services.Models.Category> GetCategoryByParentID(int id)
        {
            using (var ctx = this.Ctx)
            {
                var categorys = ctx.Categories.Where(c => c.ParentID == id).Include(c=>c.Childs);

                return categorys.ToList().AsQueryable();
            }
        }
        /// <summary>
        /// 获取类别通过 父级ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IQueryable<brand.admin.cyt369.com.services.Models.Category> GetCategoryByParentID_NoInclude(int id)
        {
            using (var ctx = this.Ctx)
            {
                var categorys = ctx.Categories.Where(c => c.ParentID == id);

                return categorys.ToList().AsQueryable();
            }
        }

        /// <summary>
        /// 获得类别集合
        /// </summary>
        /// <returns></returns>
        public IQueryable<brand.admin.cyt369.com.services.Models.Category> CategoryGets()
        {
            using (var ctx = this.Ctx)
            {
                var categorys = ctx.Categories;

                return categorys.ToList().AsQueryable();
            }
        }

        /// <summary>
        /// 保存修改
        /// </summary>
        /// <returns></returns>
        public bool SaveCategory(brand.admin.cyt369.com.services.Models.Category category)
        {
            using (var ctx = this.Ctx)
            {
                var newCategory = ctx.Categories.FirstOrDefault(c=>c.Id==category.Id);
               if(newCategory != null)
                {
                    newCategory.ParentID = category.ParentID;
                    newCategory.CategoryName = category.CategoryName;
                    newCategory.Note = category.Note;
                    newCategory.OrderNumber = category.OrderNumber;
                    ctx.SaveChanges();
                    return true;
                }
                else
                {
                    ctx.Categories.Add(category);
                    return ctx.SaveChanges() > 0;
                }
            }
        }
        /// <summary>
        /// 删除类别
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteCategory(int id)
        {
            using (var ctx = this.Ctx)
            {
                var newCategory = ctx.Categories.FirstOrDefault(c => c.Id == id);
                if (newCategory != null)
                {
                    //newCategory.IsDelete = true;
                    //ctx.SaveChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
}
